package com.dianning.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.*;
import org.apache.struts2.interceptor.SessionAware;

import com.dianning.bean.ChannelContact;
import com.dianning.bean.Credit;
import com.dianning.bean.Message;
import com.dianning.bean.Money;
import com.dianning.bean.MoneyLog;
import com.dianning.bean.Orders;
import com.dianning.bean.Profile;
import com.dianning.bean.UserInfo;
import com.dianning.bean.Visitor;
import com.dianning.comm.ChannelConfig;
import com.dianning.comm.Constant;
import com.dianning.comm.OrderObj;
import com.dianning.exception.DianningException;
import com.dianning.service.ChannelService;
import com.dianning.service.CreditService;
import com.dianning.service.InfoService;
import com.dianning.service.MoneyService;
import com.dianning.service.OrdersService;
import com.dianning.service.ProfileService;
import com.dianning.service.UserInfoService;
import com.dianning.service.VisitorService;
import com.dianning.service.web.PageList;
import com.dianning.service.web.Pages;
import com.dianning.web.interceptor.RemoteAddrAware;
import com.dianning.web.interceptor.UserSessionAware;
import com.dianning.web.servlet.UserSession;

public class ProfileAction extends BaseAction  implements SessionAware,RemoteAddrAware,UserSessionAware{

	private static final Log logger = LogFactory.getLog(ProfileAction.class);
	
	private ProfileService profileService;
	private VisitorService visitorService;
	private CreditService creditService;
	private MoneyService moneyService;
	private UserInfoService userInfoService;
	private OrdersService ordersService;
	private InfoService infoService;
	private ChannelService channelService;


	public ProfileService getProfileService() {
		return profileService;
	}

	public void setProfileService(ProfileService profileService) {
		this.profileService = profileService;
	}

	public VisitorService getVisitorService() {
		return visitorService;
	}

	public void setVisitorService(VisitorService visitorService) {
		this.visitorService = visitorService;
	}
	
	public CreditService getCreditService() {
		return creditService;
	}

	public void setCreditService(CreditService creditService) {
		this.creditService = creditService;
	}

	public MoneyService getMoneyService() {
		return moneyService;
	}

	public void setMoneyService(MoneyService moneyService) {
		this.moneyService = moneyService;
	}
	
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}	

	public OrdersService getOrdersService() {
		return ordersService;
	}

	public void setOrdersService(OrdersService ordersService) {
		this.ordersService = ordersService;
	}
	
	public InfoService getInfoService() {
		return infoService;
	}

	public void setInfoService(InfoService infoService) {
		this.infoService = infoService;
	}
	
	public ChannelService getChannelService() {
		return channelService;
	}

	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}
	
	private String remoteAddr;
	private Map session;
	
	public String getRemoteAddr() {
		return remoteAddr;
	}

	public void setRemoteAddr(String remoteAddr) {
		this.remoteAddr = remoteAddr;
	}
	public Map getSession() {
		return session;
	}
	
	public void setSession(Map session) {
		this.session = session;
	}
	
	private UserSession userSession;

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}
	
	private Profile profile;
	private UserInfo userInfo;
	private List infos;
	private List links;
	private List abouts;
	private List aptitudes;
	private List commons;
	private ChannelContact smallChannel;
	private PageList pageList;
	private String domain = Constant.DOMAIN;
	private String profileDomain = Constant.PROFILE_DOMAIN;
	private long couponQuan=ChannelConfig.COUPON_QUAN_ID;

	public Profile getProfile() {
		return profile;
	}

	public void setProfile(Profile profile) {
		this.profile = profile;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public List getInfos() {
		return infos;
	}

	public void setInfos(List infos) {
		this.infos = infos;
	}
	
	public List getLinks() {
		return links;
	}

	public void setLinks(List links) {
		this.links = links;
	}
	
	public List getAptitudes() {
		return aptitudes;
	}

	public void setAptitudes(List aptitudes) {
		this.aptitudes = aptitudes;
	}
	
	public List getCommons() {
		return commons;
	}

	public void setCommons(List commons) {
		this.commons = commons;
	}
	
	public List getAbouts() {
		return abouts;
	}

	public void setAbouts(List abouts) {
		this.abouts = abouts;
	}

	public ChannelContact getSmallChannel() {
		return smallChannel;
	}

	public void setSmallChannel(ChannelContact smallChannel) {
		this.smallChannel = smallChannel;
	}

	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}
	
	public String getProfileDomain() {
		return profileDomain;
	}

	public void setProfileDomain(String profileDomain) {
		this.profileDomain = profileDomain;
	}
	
	private String webkey;
	private String code;
	private long uid;
	private String content;
	private String contact;
	private String address;
	private long ccid;

	public String getWebkey() {
		return webkey;
	}

	public void setWebkey(String webkey) {
		this.webkey = webkey;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}
	
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public long getCcid() {
		return ccid;
	}

	public void setCcid(long ccid) {
		this.ccid = ccid;
	}

	public long getCouponQuan() {
		return couponQuan;
	}

	public void setCouponQuan(long couponQuan) {
		this.couponQuan = couponQuan;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return ERROR;
		}
	}

	public String index() {
		this.setProfile(this.getProfileService().findProfileByWebkey(this.webkey));
		if(this.getProfile() != null){
			if(this.getProfile().getShowstatus()!=1){
				return "statusprofile";
			}
			this.setUid(this.getProfile().getUid());
			this.setUserInfo(this.getUserInfoService().findUserInfoByUid(this.getProfile().getUid()));
			this.setInfos(this.getProfileService().findProfileInfos(this.getProfile().getUid(), "info", "orders", 0));
			//if(this.getProfile().getStatus()==2){
				if(this.getProfile().getCcidList()!=null){
					for(int i=0;i<this.getProfile().getCcidList().size();i++){
					ChannelContact cc = (ChannelContact) this.getProfile().getCcidList().get(i);
					cc.setInfos(this.getInfoService().findInfos(-1, this.getProfile().getUid(), cc.getId(), 1, 1, 1, "addTime", 1, 0, 5));
					}
				}
				this.setAbouts(this.getProfileService().findProfileInfos(this.getProfile().getUid(),"about", "orders", 0));
				this.setLinks(this.getProfileService().findProfileLinks(this.getProfile().getUid(), "orders", 0));
				return "vip";	
			//}
			
			/*
			try {
				Visitor v = this.getVisitorService().findVisitorByIpUid(remoteAddr, this.getProfile().getUid());
				if(v==null){
					v = new Visitor();
					v.setUid(this.getProfile().getUid());
					v.setIp(remoteAddr);
					v.setLastTime(new Date());
					this.getVisitorService().saveVisitor(v);
				}else{
					v.setLastTime(new Date());
					this.getVisitorService().updateVisitor(v);
				}
				long visitorNum = this.getVisitorService().getTodayVisitorNumByUid(this.getProfile().getUid());
				//20访问
				if(visitorNum==20){
					Credit c = this.getCreditService().findCreditByUidCodeTime(this.getProfile().getUid(),"visitor_20ip");
					//未添加
					if(c == null){
						c = this.getCreditService().findCreditByUidCode(this.getProfile().getUid(),"visitor_20ip");
						if(c == null){
							c = new Credit();
							c.setUid(this.getProfile().getUid());
							c.setCode("visitor_20ip");
							c.setCycletype(0);
							c.setPoints(Constant.VISITOR_20IP_MONEY);
							c.setAddTime(new Date());
						}else{
							c.setAddTime(new Date());
						}
						this.getCreditService().createCredit(c);
					}
					UserInfo ui = this.getUserInfoService().findUserInfoByUid(this.getProfile().getUid());
					Money m = this.getMoneyService().findMoneyByUid(ui.getId());
					m.setPoints(m.getPoints() + Constant.VISITOR_20IP_MONEY);
					m.setTotal(m.getTotal() + Constant.VISITOR_20IP_MONEY);
					MoneyLog ml = new MoneyLog();
					ml.setUid(ui.getId());
					ml.setUsername(ui.getUsername());
					ml.setLogtype("in");
					ml.setPoints(Constant.VISITOR_20IP_MONEY);
					ml.setExplain("商家网店访问人数达到20人");
					ml.setAddTime(new Date());
					this.getMoneyService().updateMoney(m, ml);
				}
				//50访问
				if(visitorNum==50){
					Credit c = this.getCreditService().findCreditByUidCodeTime(this.getProfile().getUid(),"visitor_50ip");
					//未添加
					if(c == null){
						c = this.getCreditService().findCreditByUidCode(this.getProfile().getUid(),"visitor_50ip");
						if(c == null){
							c = new Credit();
							c.setUid(this.getProfile().getUid());
							c.setCode("visitor_50ip");
							c.setCycletype(0);
							c.setPoints(Constant.VISITOR_50IP_MONEY);
							c.setAddTime(new Date());
						}else{
							c.setAddTime(new Date());
						}
						this.getCreditService().createCredit(c);
					}
					UserInfo ui = this.getUserInfoService().findUserInfoByUid(this.getProfile().getUid());
					Money m = this.getMoneyService().findMoneyByUid(ui.getId());
					m.setPoints(m.getPoints() + Constant.VISITOR_50IP_MONEY);
					m.setTotal(m.getTotal() + Constant.VISITOR_50IP_MONEY);
					MoneyLog ml = new MoneyLog();
					ml.setUid(ui.getId());
					ml.setUsername(ui.getUsername());
					ml.setLogtype("in");
					ml.setPoints(Constant.VISITOR_50IP_MONEY);
					ml.setExplain("商家网店访问人数达到50人");
					ml.setAddTime(new Date());
					this.getMoneyService().updateMoney(m, ml);
				}
			} catch (DianningException e) {
	            this.addActionError(this.getText("error.visitor.createrror"));
	            return ERROR;
	        }
	        */
			//return SUCCESS;	
		}	
		return "noprofile";
	}
	
	public String channel(){
		this.setProfile(this.getProfileService().findProfileByUid(this.uid));
		if(this.getProfile() != null){
			this.setUid(this.getProfile().getUid());
			this.setUserInfo(this.getUserInfoService().findUserInfoByUid(this.getProfile().getUid()));
			this.setLinks(this.getProfileService().findProfileLinks(this.getProfile().getUid(), "orders", 0));
			this.setSmallChannel(this.getChannelService().findChannelContactById(this.ccid));
			Pages pages = new Pages();
			pages.setPage(this.getPage());
			pages.setPerPageNum(Constant.PROFILE_PAGE_NUM);
			if (this.getTotal() > 0) {
				pages.setTotalNum(this.getTotal());
			}
			pages.setFileName("/profile.action?action=channel&uid="+uid+"&ccid="+ccid);
			List wherein = new ArrayList();
			OrderObj commend = new OrderObj();
			commend.setOrderBy("isCommend");
			commend.setAscOrDesc(1);
			OrderObj up = new OrderObj();
			up.setOrderBy("upTime");
			up.setAscOrDesc(1);
			OrderObj[] oo = {commend,up};
			this.setPageList(this.getInfoService().findInfos(this.uid, -1, this.ccid, -1, -1, -1, 1, 1, 1, "", wherein, oo, pages));
			return "channel";
		}
		return "noprofile";
	}
	
	public String aptitude(){
		this.setProfile(this.getProfileService().findProfileByUid(this.uid));
		if(this.getProfile() != null){
			this.setUid(this.getProfile().getUid());
			this.setUserInfo(this.getUserInfoService().findUserInfoByUid(this.getProfile().getUid()));
			this.setLinks(this.getProfileService().findProfileLinks(this.getProfile().getUid(), "orders", 0));
			this.setAptitudes(this.getProfileService().findProfileInfos(this.getProfile().getUid(),"aptitude", "orders", 0));
			return "aptitude";
		}
		return "noprofile";
	}
	
	public String common(){
		this.setProfile(this.getProfileService().findProfileByUid(this.uid));
		if(this.getProfile() != null){
			this.setUid(this.getProfile().getUid());
			this.setUserInfo(this.getUserInfoService().findUserInfoByUid(this.getProfile().getUid()));
			this.setLinks(this.getProfileService().findProfileLinks(this.getProfile().getUid(), "orders", 0));
			this.setCommons(this.getProfileService().findProfileInfos(this.getProfile().getUid(),"common", "orders", 0));
			return "common";
		}
		return "noprofile";
	}


	public String orders(){
		Orders o = new Orders();
		o.setUid(this.uid);
		o.setContent(this.content);
		o.setContact(this.contact);
		o.setAddress(this.address);
		o.setIsnew(1);
		o.setDelstatus(0);
		o.setAddTime(new Date());
		this.getOrdersService().saveOrders(o);
		return "orders_profile";
	}
	
	public String channelorders(){
		Orders o = new Orders();
		o.setUid(this.uid);
		o.setContent(this.content);
		o.setContact(this.contact);
		o.setAddress(this.address);
		o.setIsnew(1);
		o.setDelstatus(0);
		o.setAddTime(new Date());
		this.getOrdersService().saveOrders(o);
		return "orders_channel_profile";
	}
	
	public String aptitudeorders(){
		Orders o = new Orders();
		o.setUid(this.uid);
		o.setContent(this.content);
		o.setContact(this.contact);
		o.setAddress(this.address);
		o.setIsnew(1);
		o.setDelstatus(0);
		o.setAddTime(new Date());
		this.getOrdersService().saveOrders(o);
		return "orders_aptitude_profile";
	}
	
	public String commonorders(){
		Orders o = new Orders();
		o.setUid(this.uid);
		o.setContent(this.content);
		o.setContact(this.contact);
		o.setAddress(this.address);
		o.setIsnew(1);
		o.setDelstatus(0);
		o.setAddTime(new Date());
		this.getOrdersService().saveOrders(o);
		return "orders_common_profile";
	}
}
